package io.agora.openlive;

import android.opengl.GLES20;

import com.texeljoy.hteffect.HTEffect;
import com.texeljoy.hteffect.model.HTRotationEnum;
import io.agora.capture.framework.modules.channels.VideoChannel;
import io.agora.capture.framework.modules.processors.IPreprocessor;
import io.agora.capture.video.camera.VideoCaptureFrame;

public class MTPreprocessor implements IPreprocessor {
    private final static String TAG = MTPreprocessor.class.getSimpleName();

    public static boolean mEnabled = true;

    //todo --- hteffect start
    public static boolean isFrontCamera = true;
    public static boolean isCameraSwitched = false;
    public static boolean isRenderInit = false;
    //todo --- hteffect end

    @Override
    public VideoCaptureFrame onPreProcessFrame(VideoCaptureFrame outFrame, VideoChannel.ChannelContext context) {
        if (!mEnabled) {
            return outFrame;
        }

        //todo --- hteffect start
        if (isCameraSwitched) {
            HTEffect.shareInstance().releaseTextureOESRenderer();
            isRenderInit = false;
            isCameraSwitched = false;
        }
        if (!isRenderInit) {
            isRenderInit = HTEffect.shareInstance().initTextureOESRenderer(outFrame.format.getWidth(), outFrame.format.getHeight(),
                isFrontCamera ? HTRotationEnum.HTRotationClockwise270 : HTRotationEnum.HTRotationClockwise90,
                isFrontCamera, 5);

        }
        outFrame.textureId = HTEffect.shareInstance().processTextureOES(outFrame.textureId);


        // The texture is transformed to texture2D by beauty module.
        outFrame.format.setTexFormat(GLES20.GL_TEXTURE_2D);
        return outFrame;
        //todo --- hteffect end
    }

    @Override
    public void initPreprocessor() {
        mEnabled = true;
    }

    @Override
    public void enablePreProcess(boolean enabled) {
        mEnabled = enabled;
    }

    @Override
    public void releasePreprocessor(VideoChannel.ChannelContext context) {
        //todo --- hteffect start
        HTEffect.shareInstance().releaseTextureOESRenderer();
        isRenderInit = false;
        //todo --- hteffect end

    }


}
